package ByteTree;
import Verifier.GeneralUtil;

/* This class test the ByteTree representation */
public class Test {
	
	public static void main(String [] args) throws Exception {
		
		/* strings representing ByteTrees */
		String str = "0100000002FEF9";
		
		String str1 = "0000000003" +
						"0000000001" +
							"0100000001AF" +
						"010000000203E1" +
						"01000000022D52";
		
		String str2 = "0000000002" +
						"0000000002" +
							"01000000020102" +
							"01000000020006" +
						"01000000020005";
		
		
		String str3 = "0000000002" + 
						"0000000003" +
							"01000000020001" +
							"01000000020002" +
							"01000000020003" +
						"0000000003" +
							"01000000020004" +
							"01000000020005" +
							"01000000020006";
		
		/* creating byte arrays representing byte trees */
		byte[] ba = GeneralUtil.hexStringToByteArray(str);
		byte[] ba1 = GeneralUtil.hexStringToByteArray(str1);
		byte[] ba2 = GeneralUtil.hexStringToByteArray(str2);
		byte[] ba3 = GeneralUtil.hexStringToByteArray(str3);
		
		/* creating ByteTrees */
		ByteTree bt = new ByteTree(ba);
		ByteTree bt1 = new ByteTree(ba1);
		ByteTree bt2 = new ByteTree(ba2);
		ByteTree bt3 = new ByteTree(ba3);
		
		/* check the ByteTrees constructed from byte arrays */
		System.out.println("The ByteTree represented by the byte array " + str + " is:");
		printLeavesData(bt.getRoot());
		System.out.println("\n\n*************************************************************************************************\n");
		System.out.println("The ByteTree represented by the byte array " + str1 + " is:");
		printLeavesData(bt1.getRoot());
		System.out.println("\n\n*************************************************************************************************\n");
		System.out.println("The ByteTree represented by the byte array " + str2 + " is:");
		printLeavesData(bt2.getRoot());
		System.out.println("\n\n*************************************************************************************************\n");
		System.out.println("The ByteTree represented by the byte array " + str3 + " is:");
		printLeavesData(bt3.getRoot());
		
		System.out.println("\n\n\n--------------------------------------------------------------------------------------------------\n\n");
		
		/* check the byte arrays derived from the ByteTrees */
		System.out.print("The byte array representing the ByteTree ");
		printLeavesData(bt.getRoot());
		System.out.println(" is:\n" + GeneralUtil.byteArrayToHexString(bt.toByteArray()));
		System.out.println("\n*************************************************************************************************\n");
		System.out.print("The byte array representing the ByteTree ");
		printLeavesData(bt1.getRoot());
		System.out.println(" is:\n" + GeneralUtil.byteArrayToHexString(bt1.toByteArray()));
		System.out.println("\n*************************************************************************************************\n");
		System.out.print("The byte array representing the ByteTree ");
		printLeavesData(bt2.getRoot());
		System.out.println(" is:\n" + GeneralUtil.byteArrayToHexString(bt2.toByteArray()));
		System.out.println("\n*************************************************************************************************\n");
		System.out.print("The byte array representing the ByteTree ");
		printLeavesData(bt3.getRoot());
		System.out.println(" is:\n" + GeneralUtil.byteArrayToHexString(bt3.toByteArray()));
		
		System.out.println("\n\n\n--------------------------------------------------------------------------------------------------\n\n");
		
		/* check correctness */
		if (str.compareTo(GeneralUtil.byteArrayToHexString(bt.toByteArray())) == 0)
			System.out.println("First ByteTree is ok.");
		else
			System.out.println("First byte tree is wrong.");
		
		if (str1.compareTo(GeneralUtil.byteArrayToHexString(bt1.toByteArray())) == 0)
			System.out.println("Second ByteTree is ok.");
		else
			System.out.println("Second byte tree is wrong.");
		
		if (str2.compareTo(GeneralUtil.byteArrayToHexString(bt2.toByteArray())) == 0)
			System.out.println("Third ByteTree is ok.");
		else
			System.out.println("Third byte tree is wrong.");
		
		if (str3.compareTo(GeneralUtil.byteArrayToHexString(bt3.toByteArray())) == 0)
			System.out.println("Fourth ByteTree is ok.");
		else
			System.out.println("Fourth byte tree is wrong.");
		
	}
	
	private static void printLeavesData(Node n) {
		if (n.isLeaf())
		{ 
			Leaf l = (Leaf) n;
			System.out.print("leaf(");
			System.out.print(GeneralUtil.byteArrayToHexString(l.getData()) + ")");
		}
		
		else
		{
			System.out.print("node(");
			for(Node c: n.getChildren()) {
				printLeavesData(c);
			}
			System.out.print(")");
		}
	}
}